package societyGames.codeAlong.model
{
import com.societyGames.utility.XMLDictionary;

import flash.events.Event;
import flash.filesystem.File;
import flash.filesystem.FileStream;

import societyGames.document.model.Document;
import societyGames.xmlFragment.XMLContainer;

public class ModulesDocument extends Document
{
  private static var _instance:ModulesDocument;

  [Bindable]
  public var modulesContainer:ModulesContainer;
  private var _modulesXML:XML;

  [Bindable]
  public var solutionsContainer:SolutionsContainer;
  private var _solutionsXML:XML;

  private static const MODULES_FILE:Object = new Object();
  private static const SOLUTIONS_FILE:Object = new Object();
  private var _currentFileType:Object = MODULES_FILE;

  private var _xmlDictionary:XMLDictionary;

  public function ModulesDocument()
  {
    ModulesDocument.instance = this;
  }

  public static function get instance():ModulesDocument
  {
    return _instance;
  }

  [Bindable]
  public static function set instance(value:ModulesDocument):void
  {
    _instance = value;
  }

  [Bindable]
  public function set xmlDictionary(value:XMLDictionary):void
  {
    _xmlDictionary = value;
  }

  public function get xmlDictionary():XMLDictionary
  {
    return _xmlDictionary;
  }

  //Load from the specified file.
  override public function load(file:File):void
  {
    _currentFileType = MODULES_FILE;
    super.load(file);

    var tempFile:File = this._file;
    var solutionsFile:File = tempFile.resolvePath("../Solutions.xml");
    _currentFileType = SOLUTIONS_FILE;
    super.load(solutionsFile);

    this._file = tempFile;
  }

  override protected function loadFromFileStream(fileStream:FileStream):void
  {
    var inputString:String = fileStream.readUTFBytes(fileStream.bytesAvailable);
    inputString = inputString.replace(/[\r]/g, "\n");
    switch (_currentFileType)
    {
      case MODULES_FILE:
      {
        _modulesXML = XML(inputString);
        modulesContainer = new ModulesContainer();
        modulesContainer.addEventListener(XMLContainer.CHANGED, onXMLChanged);
        modulesContainer.initialize(modulesContainer, _modulesXML);

        xmlDictionary = new XMLDictionary(_modulesXML.entry);
        break;
      }
      case SOLUTIONS_FILE:
      {
        _solutionsXML = XML(inputString);
        solutionsContainer = new SolutionsContainer();
        solutionsContainer.addEventListener(XMLContainer.CHANGED, onXMLChanged);
        solutionsContainer.initialize(solutionsContainer, _solutionsXML);
        break;
      }
    }
  }

  //Load from the specified file.
  override public function save(file:File = null):void
  {
    //Save modules file.
    _currentFileType = MODULES_FILE;
    super.save(file);

    //Save solutions file.
    var tempFile:File = this._file;
    var solutionsFile:File = tempFile.resolvePath("../Solutions.xml");
    _currentFileType = SOLUTIONS_FILE;
    super.save(solutionsFile);

    //Set the file to point back to the original
    this._file = tempFile;
  }

  override protected function saveToFileStream(fileStream:FileStream):void
  {
    var outputString:String;
    switch (_currentFileType)
    {
      case MODULES_FILE:
      {
        outputString = '<?xml version="1.0" encoding="utf-8"?>\n';
        outputString += this._modulesXML.toXMLString();
        outputString = outputString.replace(/[\r]/g, "\n");
        fileStream.writeUTFBytes(outputString);
        break;
      }
      case SOLUTIONS_FILE:
      {
        outputString = '<?xml version="1.0" encoding="utf-8"?>\n';
        outputString += this._solutionsXML.toXMLString();
        outputString = outputString.replace(/[\r\n]/g, "\n");
        fileStream.writeUTFBytes(outputString);
        break;
      }
    }
  }

  private function onXMLChanged(event:Event):void
  {
    isDirty = true;
  }
}
}